package com.gh.ssyx.controller;

import com.gh.ssyx.common.result.Result;
import com.gh.ssyx.model.acl.Permission;
import com.gh.ssyx.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Eric Fang
 * @date 2023/6/19 21:10
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/acl/permission")
@CrossOrigin
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    //查询所有菜单
    @ApiOperation("查询所有菜单")
    @GetMapping
    public Result getPermissionList(){
        List<Permission> list = permissionService.queryAllPermission();
        return Result.ok(list);
    }

    //添加菜单
    @ApiOperation("添加菜单")
    @PostMapping("/save")
    public Result addPermission(@RequestBody Permission permission){
        boolean b = permissionService.save(permission);
        return b ? Result.ok(null) : Result.fail(null);
    }

    //修改菜单
    @ApiOperation("修改菜单")
    @PutMapping("/update")
    public Result updatePermission(@RequestBody Permission permission){
        boolean b = permissionService.updateById(permission);
        return b ? Result.ok(null) : Result.fail(null);
    }

    //删除菜单
    @ApiOperation("删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result removePermission(@PathVariable Long id){
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

    //查看某个角色的权限列表
    @ApiOperation("查看某个角色的权限列表")
    @GetMapping("/toAssign/{id}")
    public Result toAssignPermission(@PathVariable Long id){
        //查询所有的菜单和查询角色已经分配过的菜单
        List<Permission> permissionList = permissionService.toAssignPermission(id);
        return Result.ok(permissionList);
    }

    //给某个角色授权
    @ApiOperation("给某个用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long roleId,
                           @RequestParam("permissionId") Long[] permissionIds){
        permissionService.saveRolePermission(roleId,permissionIds);
        return Result.ok(null);
    }
}
